package com.byq.ai.agentlab.service;


import com.byq.ai.agentlab.po.Conversation;
import com.byq.ai.agentlab.po.MyConversation;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ConversationService {
    @PersistenceContext
    EntityManager entityManager;


    public Conversation getConversation(String id){
        MyConversation rst = entityManager.find(MyConversation.class, id);
        return rst.toConversation();
    }
    @Transactional
    public void save(Conversation con){
        MyConversation  my = new MyConversation();
        my.fromConversation(con);

        entityManager.persist(my);
    }
    public List<Conversation> getAll(){
        List<MyConversation> ls = entityManager.createQuery("from MyConversation", MyConversation.class).getResultList();
        List<Conversation> rst = new ArrayList<>();
        for(MyConversation m : ls){
            Conversation con = m.toConversation();
            rst.add(con);
        }
        return rst;


    }
}
